package com.example.customcomwidget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListPopupWindow;
import android.widget.ListView;
import android.widget.PopupWindow;


/**
 * @author hdz
 * @date 2021/08/11 9:28 AM
 */
public class ListPopupWindowUtil {

    private ListPopupWindow listPopupWindow;
    private OnVisibleChangeListener visibleChangeListener;
    private View anchor;

    public ListPopupWindowUtil(final Builder builder) {
        listPopupWindow = new ListPopupWindow(builder.context);
        listPopupWindow.setAnchorView(builder.anchor);
        listPopupWindow.setAdapter(builder.adapter);
        listPopupWindow.setModal(true);

        this.anchor = builder.anchor;
        this.visibleChangeListener = builder.visibleChangeListener;

        int w = builder.anchor.getWidth() / 2;
        int h = builder.anchor.getHeight() * 2;
        listPopupWindow.setWidth(w);
        if (builder.height == 0) {
            listPopupWindow.setHeight(h);
        } else {
            listPopupWindow.setHeight(builder.height);
        }
        int hOffset = builder.touchX - w / 2;
        listPopupWindow.setHorizontalOffset(hOffset);
        listPopupWindow.setVerticalOffset(-builder.anchor.getHeight() / 4);

        if (builder.bgDrawable != null) {
            listPopupWindow.setBackgroundDrawable(builder.bgDrawable);
        }

        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (builder.clickListener != null) {
                    builder.clickListener.onItemClick(parent, view, position, id);
                }
                listPopupWindow.dismiss();
            }
        });

        listPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (builder.visibleChangeListener != null) {
                    builder.visibleChangeListener.onVisibleChanged(false);
                }
            }
        });
    }

    public void show(int touchX) {
        int w = this.anchor.getWidth() / 2;
        int hOffset = touchX - w / 2;
        listPopupWindow.setHorizontalOffset(hOffset);

        listPopupWindow.show();

        if (this.visibleChangeListener != null) {
            this.visibleChangeListener.onVisibleChanged(true);
        }

        ListView listView = listPopupWindow.getListView();
        if (listView != null) {
            ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) listView.getLayoutParams();
            mlp.setMargins(0, 10, 0, 10);
            listView.setVerticalFadingEdgeEnabled(true);
            listView.setFadingEdgeLength(50);
            listView.setVerticalScrollBarEnabled(false);
        }
    }


    public static class Builder {
        private final Context context;
        private final View anchor;
        private final ListAdapter adapter;

        private OnVisibleChangeListener visibleChangeListener;
        private Drawable bgDrawable;
        private AdapterView.OnItemClickListener clickListener;
        private int touchX;
        private int height = 0;

        public Builder(View anchor, ListAdapter adapter) {
            this.context = anchor.getContext();
            this.anchor = anchor;
            this.adapter = adapter;
        }

        public Builder setHeight(int h) {
            this.height = h;
            return this;
        }

        public Builder setTouchX(int x) {
            this.touchX = x;
            return this;
        }

        public Builder setBackgroundDrawable(Drawable drawable) {
            this.bgDrawable = drawable;
            return this;
        }

        public Builder setOnVisibleChangeListener(OnVisibleChangeListener listener) {
            this.visibleChangeListener = listener;
            return this;
        }

        public Builder setOnItemClickListener(AdapterView.OnItemClickListener listener) {
            this.clickListener = listener;
            return this;
        }

        public ListPopupWindowUtil builder() {
            return new ListPopupWindowUtil(this);
        }
    }


    public interface OnVisibleChangeListener {
        void onVisibleChanged(boolean visible);
    }
}
